package de.lmu.ifi.pst.uwejsf.framework;

import java.util.HashMap;
import java.util.List;
import java.util.Vector;

public abstract class QueryBean extends NavigationNodeBean {
	protected java.util.Map<String, java.util.List<de.lmu.ifi.pst.uwejsf.framework.NavigationRule>> navigationRules = new HashMap<String, List<NavigationRule>>();

	protected void addNavigationRule(String sourceView,
			NavigationRule navigationRule) {
		if (navigationRules.get(sourceView) == null)
			navigationRules.put(sourceView, new Vector<NavigationRule>());
		navigationRules.get(sourceView).add(navigationRule);
	}

	protected QueryHandler queryHandler = null;

	public QueryHandler getQueryHandler() {
		return queryHandler;
	}

	public void setQueryHandler(QueryHandler queryHandler) {
		this.queryHandler = queryHandler;
	}

	protected String navigate() {
		// don't handle submit orders that were produced by selection elements
		getUWENavigator().unRegisterSubmitOrder();
		java.util.List<de.lmu.ifi.pst.uwejsf.framework.NavigationRule> rules = navigationRules
				.get(sourceView);
		for (de.lmu.ifi.pst.uwejsf.framework.NavigationRule rule : rules) {
			if (rule.checkCondition())
				return rule.follow();
		}
		return null;

	}

	private String sourceView = null;

	public void setSourceView(javax.faces.event.ActionEvent ev) {
		sourceView = (String) ev.getComponent().getAttributes().get(
				"sourceView");
	}

	public abstract String search();
	public abstract List<?> getResult();
	public abstract int getResultSize();

}
